//
//  HHScrollPageView.m
//  Flower
//
//  Created by 花花 on 2016/12/5.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHScrollPageView.h"


@interface HHScrollPageView ()
@property(nonatomic, strong) HHSegmentStyle *segmentStyle;
@property(nonatomic, weak) HHScrollSegmentView *segmentView;
@property(nonatomic, weak) HHContentView *contentView;


@property(nonatomic, weak) UIViewController *parentViewController;
@property(nonatomic, strong) NSArray *childVcs;
@property(nonatomic, strong) NSArray *titlesArray;

@end
@implementation HHScrollPageView

- (instancetype)initWithFrame:(CGRect)frame segmentStyle:(HHSegmentStyle *)segmetStyle titles:(NSArray<NSString *>*)titles parentViewController:(UIViewController *)parentViewController delegate:(id<HHScrollPageViewDelegate>)delegate{

    if (self = [super initWithFrame:frame]) {
        self.segmentStyle = segmetStyle;
        self.titlesArray  = titles.copy;
        self.parentViewController = parentViewController;
        self.delegate = delegate;
        [self commonInit];
    }

    return self;
}
-(void)commonInit{
    
    //触发lanjiaz
    self.segmentView.backgroundColor =[UIColor whiteColor];
    self.contentView.backgroundColor =[UIColor whiteColor];


}


//给外界设置选中的下标的方法
- (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated{
    [self.segmentView setSelectedIndex:selectedIndex animated:animated];

}

//给外界重新设置的标题的方法(同事会重新加载页面的内容)
-(void)reloadWithNewTitles:(NSArray<NSString *>*)newTitles{
    self.titlesArray = nil;
    self.titlesArray = newTitles.copy;
    
    [self.segmentView reloadTitlesWithNewTitles:self.titlesArray];
    
    [self.contentView reload];
    

}

-(HHScrollSegmentView *)segmentView{
    if (!_segmentView) {
        __weak typeof (self) weakSelf = self;
        
        HHScrollSegmentView  *segmentView =[[HHScrollSegmentView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.segmentStyle.segmentHeight) segmentStyle:self.segmentStyle delegate:self.delegate titles:self.titlesArray titleDidClick:^(HHTitleView *titleView, NSInteger index) {
            
            [weakSelf.contentView setContentOffSet:CGPointMake(weakSelf.contentView.bounds.size.width *index, 0.0) animated:weakSelf.segmentStyle.isAnimatedContentViewWhenTitleClicked];
        }];
        
        [self addSubview:segmentView];
        _segmentView = segmentView;
    }
    
    return _segmentView;
    
}
-(HHContentView *)contentView{

    if (!_contentView) {
        HHContentView *contentView =[[HHContentView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.segmentView.frame), self.width, self.height-CGRectGetMaxY(self.segmentView.frame)) segmentView:self.segmentView parentViewController:self.parentViewController delegate:self.delegate];
        
        [self addSubview:contentView];
        _contentView = contentView;
    }

    return _contentView;

}

-(NSArray *)childVcs{
    
    if (!_childVcs) {
        _childVcs = [NSArray array];
    }
    
    return _childVcs;
    
}

-(NSArray *)titlesArray{
    
    if (!_titlesArray) {
        _titlesArray = [NSArray array];
    }
    
    return _titlesArray;
    
}
-(void)setTempBtnOnClock:(tempBtnOnClick)tempBtnOnClock{

    _tempBtnOnClock = tempBtnOnClock;
    self.segmentView.tempBtnOnClick = tempBtnOnClock;

}
@end
